// pages/shops/shopscart/shopscart.js
const SWIPE_WIDTH = 120;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartList: [],
    totalPrice: 0,
    isAllChecked: false
  },

  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let cart = wx.getStorageSync('cart') || [];
    cart = cart.map(item => ({
      ...item,
      id: Number(item.id),
      checked: item.checked !== false,
      x: 0 // 初始化滑动位置
    }));
    this.setData({
      cartList: cart
    });
    this.updateTotalPrice();
    this.updateAllChecked();
  },

  toggleCheck(e) {
    const id = Number(e.currentTarget.dataset.id);
    let cart = this.data.cartList.map(item =>
      item.id === id ? { ...item, checked: !item.checked } : item
    );
    this.setData({ cartList: cart });
    wx.setStorageSync('cart', cart);
    this.updateTotalPrice();
    this.updateAllChecked();
  },

  toggleAllCheck() {
    const isAllChecked = !this.data.isAllChecked;
    let cart = this.data.cartList.map(item => ({ ...item, checked: isAllChecked }));
    this.setData({ cartList: cart, isAllChecked });
    wx.setStorageSync('cart', cart);
    this.updateTotalPrice();
  },

  updateAllChecked() {
    const isAllChecked = this.data.cartList.length > 0 && this.data.cartList.every(item => item.checked);
    this.setData({ isAllChecked });
  },

  updateTotalPrice() {
    const total = this.data.cartList
      .filter(item => item.checked)
      .reduce((sum, item) => sum + item.price * item.num, 0);
    this.setData({ totalPrice: total.toFixed(2) });
  },

  increaseNum(e) {
    const id = Number(e.currentTarget.dataset.id);
    let cart = this.data.cartList;
    let index = cart.findIndex(item => item.id === id);
    if (index > -1) {
      cart[index].num += 1;
      this.setData({ cartList: cart });
      wx.setStorageSync('cart', cart);
      this.updateTotalPrice();
    }
  },

  decreaseNum(e) {
    const id = Number(e.currentTarget.dataset.id);
    let cart = this.data.cartList;
    let index = cart.findIndex(item => item.id === id);
    if (index > -1 && cart[index].num > 1) {
      cart[index].num -= 1;
      this.setData({ cartList: cart });
      wx.setStorageSync('cart', cart);
      this.updateTotalPrice();
    }
  },

  deleteItem(e) {
    const id = Number(e.currentTarget.dataset.id);
    let cart = this.data.cartList.filter(item => item.id !== id);
    this.setData({ cartList: cart });
    wx.setStorageSync('cart', cart);
    this.updateTotalPrice();
  },

  batchDelete() {
    let cart = this.data.cartList.filter(item => !item.checked);
    this.setData({ cartList: cart });
    wx.setStorageSync('cart', cart);
    this.updateTotalPrice();
    this.updateAllChecked();
  },

  checkout() {
    const selected = this.data.cartList.filter(item => item.checked);
    if (selected.length === 0) {
      wx.showToast({ title: '请选择要结算的商品', icon: 'none' });
      return;
    }
    
    // 保存选中的商品作为临时订单
    wx.setStorageSync('tempOrder', selected);
    
    // 更新购物车，移除选中的商品
    const newCart = this.data.cartList.filter(item => !item.checked);
    wx.setStorageSync('cart', newCart);
    
    // 跳转到订单确认页面
    wx.navigateTo({
      url: '/pages/order/payment'
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    const cart = wx.getStorageSync('cart') || [];
    this.setData({
      cartList: cart
    });
  },

  onMovableChange(e) {
    const id = Number(e.currentTarget.dataset.id);
    const x = e.detail.x;
    let cart = this.data.cartList.map(item =>
      item.id === id ? { ...item, x } : { ...item, x: 0 }
    );
    this.setData({ cartList: cart });
  },

  onMovableTouchEnd(e) {
    const id = Number(e.currentTarget.dataset.id);
    const x = e.detail.x;
    let cart = this.data.cartList.map(item => {
      if (item.id === id) {
        // 滑动超过40rpx就展开，否则收回
        return { ...item, x: x < -100 ? -SWIPE_WIDTH : 0 };
      }
      return { ...item, x: 0 };
    });
    this.setData({ cartList: cart });
  },
})